package com.aiit.carinsurance2.controller;

import com.aiit.carinsurance2.pojo.Courier;
import com.aiit.carinsurance2.pojo.User;
import com.aiit.carinsurance2.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Random;

@Controller
public class CourierController {
    @Autowired
    private CourierService courierService;

    @GetMapping("courier")
    public String courier(){
        return "courier";
    }
    @PostMapping("courier")
    public String courierwrite(Courier courier, HttpSession session){
        Object user = session.getAttribute("user");
        User u = (User) user;

        String states = "正在运输中";
        Random random = new Random();
        int courierid = random.nextInt(10000000);
        courier.setUid(u.getUid());
        courier.setStates(states);
        courier.setCourierid(courierid);
        int rlt = courierService.insert(courier);
        return "fuwudating";
    }
    @GetMapping("courierlook")
    public ModelAndView look(HttpSession session){
        Object user = session.getAttribute("user");
        User u = (User) user;
        Integer uid = u.getUid();
        Courier queryallcourier = courierService.queryallcourier(uid);
        System.out.println(queryallcourier.getRecipientname());
        ModelAndView mav = new ModelAndView();
        mav.addObject("courier",queryallcourier);
        mav.setViewName("courierlook");
        return mav;
    }
}
